package com.github.catvod.spider.merge;

import android.text.TextUtils;
import java.io.IOException;
import java.util.Map;
import okhttp3.Call;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/* loaded from: classes.dex */
class g {
    private final L BQ;
    private final String I;
    private String V0;
    private Request W;
    private final String h;
    private Object j;
    private final Map<String, String> oC;
    private final Map<String, String> q;
    private Request.Builder u;

    g(String str, String str2, Map<String, String> map, Map<String, String> map2, L l) {
        this(str, str2, null, map, map2, l);
    }

    private void I() {
        Map<String, String> map = this.oC;
        if (map != null) {
            for (String str : map.keySet()) {
                this.u.addHeader(str, this.oC.get(str));
            }
        }
    }

    private void V0() {
        this.u = new Request.Builder();
        String str = this.h;
        str.hashCode();
        if (str.equals("GET")) {
            q();
        } else if (str.equals("POST")) {
            this.u.post(j());
        }
        this.u.url(this.V0);
        Object obj = this.j;
        if (obj != null) {
            this.u.tag(obj);
        }
        if (this.oC != null) {
            I();
        }
        this.W = this.u.build();
    }

    private RequestBody j() {
        if (!TextUtils.isEmpty(this.I)) {
            return RequestBody.create(MediaType.parse("application/json; charset=utf-8"), this.I);
        }
        FormBody.Builder builder = new FormBody.Builder();
        Map<String, String> map = this.q;
        if (map != null) {
            for (String str : map.keySet()) {
                builder.add(str, this.q.get(str));
            }
        }
        return builder.build();
    }

    private void q() {
        if (this.q != null) {
            this.V0 += "?";
            for (String str : this.q.keySet()) {
                this.V0 += str + "=" + this.q.get(str) + "&";
            }
            this.V0 = this.V0.substring(0, r0.length() - 1);
        }
    }

    void h(OkHttpClient okHttpClient) {
        Call newCall = okHttpClient.newCall(this.W);
        try {
            Response execute = newCall.execute();
            L l = this.BQ;
            if (l != null) {
                l.h(newCall, execute);
            }
        } catch (IOException e) {
            L l2 = this.BQ;
            if (l2 != null) {
                l2.onError(newCall, e);
            }
        }
    }

    public void oC(Object obj) {
        this.j = obj;
    }

    g(String str, String str2, String str3, Map<String, String> map, L l) {
        this(str, str2, str3, null, map, l);
    }

    private g(String str, String str2, String str3, Map<String, String> map, Map<String, String> map2, L l) {
        this.j = null;
        this.h = str;
        this.V0 = str2;
        this.I = str3;
        this.q = map;
        this.oC = map2;
        this.BQ = l;
        V0();
    }
}
